package com.caigou.modules.channel.ui

import android.os.Bundle
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.container.BaseFragment
import com.caigou.modules.channel.databinding.ActivityChannelBinding
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

/**
 * @author #Suyghur.
 * Created on 2021/10/11
 */
@Route(path = RouterPath.Channel.PAGE_CHANNEL)
@AndroidEntryPoint
class ChannelActivity : BaseActivity() {

    private lateinit var binding: ActivityChannelBinding
    private var teamFragment: BaseFragment? = null
    private var medalFragment: BaseFragment? = null
    private var blackHouseFragment: BaseFragment? = null
    private var contactFragment: BaseFragment? = null

    @Autowired(name = AppConstants.TARGET_FRAGMENT_TAG)
    lateinit var targetFragmentTag: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityChannelBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.channelFlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)
        switchFragment(targetFragmentTag)
    }

    override fun switchFragment(tag: String) {
        when (tag) {
            RouterPath.Channel.TAG_TEAM -> {
                if (teamFragment == null) {
                    teamFragment = TeamFragment()
                }
                switchFragment(teamFragment!!, tag)
            }
            RouterPath.Channel.TAG_MEDAL -> {
                if (medalFragment == null) {
                    medalFragment = MedalFragment()
                }
                switchFragment(medalFragment!!, tag)
            }
            RouterPath.Channel.TAG_BLACK_HOUSE -> {
                if (blackHouseFragment == null) {
                    blackHouseFragment = BlackHouseFragment()
                }
                switchFragment(blackHouseFragment!!, tag)
            }
            RouterPath.Channel.TAG_CONTACT -> {
                if (contactFragment == null) {
                    contactFragment = ContactFragment()
                }
                switchFragment(contactFragment!!, tag)
            }
        }
    }


    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        ev?.let {
            if (it.action == MotionEvent.ACTION_DOWN) {
                val v = currentFocus
                if (isShouldHideInput(v, it)) {
                    val imm =
                        this@ChannelActivity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(v!!.windowToken, 0)
                }
                return super.dispatchTouchEvent(it)
            }
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        return if (window.superDispatchTouchEvent(ev)) {
            true
        } else onTouchEvent(ev)
    }

    override fun onBackPressed() {
        if (fragmentStack.isNotEmpty() && fragmentStack.size > 1) {
            when (fragmentStack.last) {
//                RouterPath.InfoFragmentTag.TAG_MODIFY -> modifyPwdFragment?.onBack()
            }
            fragmentStack.removeLast()
            switchFragment(fragmentStack.last)
        } else {
            super.onBackPressed()
        }
    }
}